package com.situ.javase.day8;

import org.junit.Test;

public class StringDemo {
	
	@Test
	public void test1() {
		// 如果常量池中没有"abc"这个字符串，就会在常量池中new一个"abc"字符串
		String str1 = "abc";
		String str2 = new String("abc");
		// 在常量池中有"abc"这样一个字符串，就直接指向常量池中已有的字符串
		String str3 = "abc";
		
		// 字符串类型==比较的是地址，equals比较的是内容
		System.out.println(str1 == str2);// false
		System.out.println(str1 == str3);// true
		System.out.println(str2 == str3);// false
	}
	
	@Test
	public void test2() {
		String str1 = new String("abc");
		String str2 = new String("Abc");
		System.out.println(str1 == str2);// false
		System.out.println(str1.equals(str2));// false
		System.out.println(str1.equalsIgnoreCase(str2));// true
	}
	
	@Test
	public void test3() {
		String str = "java AndroidA";
		System.out.println(str.length());// 13
		char[] array = str.toCharArray();// ['j','a','v',]
		char ch = str.charAt(5);
		System.out.println(ch);// A
		
		System.out.println(str.indexOf('A'));//5
		//如果此字符串中没有这样的字符，则返回 -1。 
		System.out.println(str.indexOf('X'));// -1
		System.out.println(str.indexOf('A', 6));// 12
		System.out.println(str.indexOf("And"));// 5
		
		System.out.println(str.lastIndexOf('A'));// 12
	}
	
	@Test
	public void test4() {
		String str = "java AndroidA";
		System.out.println(str.contains("And"));// true
		System.out.println(str.startsWith("java"));// true
		System.out.println(str.startsWith("And", 5));// true
		System.out.println(str.endsWith("oidA"));// true
		// return value.length == 0;
		System.out.println(str.isEmpty());// false
		System.out.println("".isEmpty());// true
	}
	
	@Test
	public void test55() {
		// String str = "java And";
		char[] array = {'j', 'a', 'v', 'a', ' ', 'A', 'n', 'd'};
		System.out.println(array.toString());
		String str = new String(array);
		System.out.println(str);
		char[] charArray = str.toCharArray();
		for (int i = 0; i < charArray.length; i++) {
			System.out.println(charArray[i]);
		}
	}
	
	@Test
	public void test56() {
		String str = "java AndroidA";
		String newStr = str.replace('A', 'B');
		System.out.println(str);// java AndroidA
		System.out.println(newStr);// java BndroidB
	}
	
	@Test
	public void test66() {
		String str = "java,Android,Python";
		String[] array = str.split(",");
		for (int i = 0; i < array.length; i++) {
			System.out.println(array[i]);
		}
		// foreach 增强的for循环，不需要关心下标
		for (String string : array) {
			System.out.println(string);
		}
	}
	
	@Test
	public void test67() {
		String str = "java AndroidA";
		System.out.println(str.substring(5));// AndroidA
		System.out.println(str.substring(5, 8));// And
	}
	
	@Test
	public void test77() {
		String str = "   java AndroidA     ";
		System.out.println(str);
		System.out.println(str.trim());
	}
	
	
	@Test
	public void test666() {
		String str1 = "Hello";
		String str2 = "Java";
		String str3 = "Android";
		String str4 = "Python";
		//因为String长度是不可改变的，每一次拼接的时候都会new一个新的字符串对象
		String str = str1 + str2 + str3 + str4;
		System.out.println(str);
		
		StringBuilder builder = new StringBuilder(str1);// Hello
		builder.append(str2);// HelloJava
		builder.append(str3);// HelloJavaAndroid
		builder.append(str4);// HelloJavaAndroidPython
		System.out.println(builder);
		
		StringBuilder builder2 = new StringBuilder();
		builder2.append(str1).append(str2).append(str3).append(str4);
		System.out.println(builder2);
	}
}
